Ext.define('jwawa.erp.login.LoginHandler', {
    extend: 'jwawa.erp.framework.ShellHandler',
    entities: ['sso_login_accounts'],
    ref:{
        loginFormPanel: 'form[itemId = loginFormPanel]',
        loginNameField: '#loginNameField',
        confirmButton: 'form[itemId = loginFormPanel] button[action=confirm]',
        resetButton:'form[itemId = loginFormPanel] button[action=reset]'
    },
    bindHandler: function() {
        var me = this;

        me.callParent(arguments);
//        var loginFormPanel = view.down('form[itemId = loginFormPanel]');

        me.confirmButton.on("click", function(btn, e, scope) {
        var loginForm = me.loginFormPanel.getForm();

            if (loginForm.isValid()) {
//                loginForm.doAction("jwawaSubmit", {
//                    success: function(form, action) {
//                        window.location = action.result["data"];
//                    }
//                });

                loginForm.submit({
                    success: function(form, action) {
                        window.location = action.result["data"];
                    }
                });
            }
//                   var loginNameField = loginForm.findField('loginName');
//                    alert (loginNameField.getValue());
//                    loginNameField.setValue(111);
        });

        me.resetButton.on("click", function(btn, e, scope) {
            var loginForm = me.loginFormPanel.getForm();
            loginForm.reset();
        });

        me.loginFormPanel.addListener('render', function(){
            Ext.create('Ext.util.KeyNav', me.loginFormPanel.el, {
                "enter" : function(){
                    var el = Ext.FocusManager.focusedCmp;
                    if(el.kbSubmit){
                        el.blur();
                        me.confirmButton.fireEvent('click');
                    }else if (el.nextSibling('field')) {
                        el.nextSibling('field').focus();
                    }
                },
                scope : me
            });

            me.loginNameField.focus(true, DEFAULT_FOCUS_DEFER_TIME);
        });
    }
});